package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：daqiang
 * @date ：Created in 2019/12/1
 * @description ：
 * @version: 1.0
 */

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //1. 开始分页(把页面和每页条数封装成Page，并存储到ThreadLocal中)
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //2. 条件查询(请求会被拦截器拦截，根据page参数和映射文件中的sql语句拼接 分页查询的sql语句)
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        //3. 返回结构
        return new PageResult(page.getTotal(), page);
    }

    @Override
    @Transactional
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {

        roleDao.add(role);
        setPermissionIds(role,permissionIds);
        setMenuIds(role,menuIds);
    }

    public void setPermissionIds(Role role, Integer[] permissionIds){
        if(role.getId() != null && permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                roleDao.setPermissionIds(role.getId(),permissionId);
            }
        }
    }

    public void setMenuIds(Role role, Integer[] menuIds){
        if(role.getId() != null && menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                roleDao.setMenuIds(role.getId(),menuId);
            }
        }
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsById(Integer id) {
        return roleDao.findPermissionIdsById(id);
    }

    @Override
    public List<Integer> findMenuIdsById(Integer id) {
        return roleDao.findMenuIdsById(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.edit(role);
        roleDao.delRelationWithPermission(role.getId());
        roleDao.delRelationWithMenu(role.getId());
        setPermissionIds(role,permissionIds);
        setMenuIds(role,menuIds);
    }

    @Override
    public void delById(Integer id) {
        long count1 = roleDao.findCountPermissionById(id);
        if(count1 > 0){
            throw new RuntimeException("该角色被权限所关联，不能删除！！！");
        }
        long count2 = roleDao.findCountMenuById(id);
        if(count2 > 0){
            throw new RuntimeException("该角色被菜单所关联，不能删除！！！");
        }
        //2. 删除角色与权限的关系
        roleDao.delRelationWithPermission(id);
        //2. 删除角色与菜单的关系
        roleDao.delRelationWithMenu(id);
        //3. 删除检查组
        roleDao.delById(id);
    }


}
